(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.service.viewController', viewController);
	viewController.$inject = [ '$scope', 'Constants', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'serviceService','modalService', '$filter' ];
	function viewController($scope, Constants, layoutConfig, validatorConfig, gridService, authService, serviceService, modalService, $filter) {
		var ctrl = this, serviceInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;

		ctrl.init = function() {
			ctrl.serviceStatusOptions = serviceService.getServiceStatusOptions();
			ctrl.serviceTypeOptions = serviceService.getServiceTypeOptions();
			var params = {
				serviceCode : serviceInfo.serviceCode
			};
			//forCmpnt
			ctrl.gridOptionsCmpnt = gridService.createGridOptions($scope, ctrl.layoutConfig.formConfigs.serviceCmpnt.tableConfigs.table, paginationChangedCmpnt, rowSelectionChangedCmpnt, sortChangedCmpnt);
			ctrl.gridOptionsCmpnt.enableRowHeaderSelection = false;
			ctrl.gridOptionsCmpnt.data = 'vm.searchCriteriaCmpnt.list';
			ctrl.searchCriteriaCmpnt = ctrl.gridOptionsCmpnt.defaultSearchCriteria;
			ctrl.searchCmpnt();
			//for Event
			ctrl.gridOptionsEvent = gridService.createGridOptions($scope, ctrl.layoutConfig.formConfigs.serviceEvent.tableConfigs.table, paginationChangedEvent, rowSelectionChangedEvent, sortChangedEvent);
			ctrl.gridOptionsEvent.data = 'vm.searchCriteriaEvent.list';
			ctrl.searchCriteriaEvent = ctrl.gridOptionsEvent.defaultSearchCriteria;
			ctrl.searchEvent();
			return authService.view(params).then(function(response) {
				ctrl.service = response.data || serviceInfo;
			}, angular.noop);
		};
		ctrl.viewCmpnt = function() {
			if (!ctrl.selectedRowsCmpnt || ctrl.selectedRowsCmpnt.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			modalService.openCustomDialogs({
				'params' : ctrl.selectedRowsCmpnt[0],
				'dialogsModule' : 'cda.service.serviceCmpnt'
				}
			);
		};
		ctrl.searchCmpnt = function() {
			ctrl.searchCriteriaCmpnt.list = [];
			ctrl.searchCriteriaCmpnt.serviceCode = $filter('encode')(serviceInfo.serviceCode);
			authService.searchCmpnt(ctrl.searchCriteriaCmpnt).then(
			function(response) {
				angular.merge(ctrl.searchCriteriaCmpnt, response.data);
				gridService.updateGridOptions(ctrl.searchCriteriaCmpnt, ctrl.gridOptionsCmpnt);
			},angular.noop);
		};
		function paginationChangedCmpnt(pageNum, pageSize) {
			ctrl.searchCriteriaCmpnt.pageNum = pageNum;
			ctrl.searchCriteriaCmpnt.pageSize = pageSize;
			ctrl.searchCriteriaCmpnt.list = [];
			ctrl.searchCmpnt();
		}
		function rowSelectionChangedCmpnt(eventRow, selectedRows) {
			ctrl.selectedRowsCmpnt = selectedRows;
		}
		function sortChangedCmpnt(sortColumns) {
			// now just support sort by one column
			ctrl.searchCriteriaCmpnt.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteriaCmpnt.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.searchCmpnt();
		}
		
		ctrl.viewEventNotifyTplParam = function() {
			if (!ctrl.selectedRowsEvent || ctrl.selectedRowsEvent.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			modalService.openCustomDialogs({
				'params' : ctrl.selectedRowsEvent[0],
				'dialogsModule' : 'cda.service.serviceEventNotifyTplParam'
			});
		};
		ctrl.viewEventNotifyTpl = function() {
			if (!ctrl.selectedRowsEvent || ctrl.selectedRowsEvent.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			modalService.openCustomDialogs({
				'params' : ctrl.selectedRowsEvent[0],
				'dialogsModule' : 'cda.service.serviceEventNotifyTpl'
			});
		};
		ctrl.viewEvent = function() {
			if (!ctrl.selectedRowsEvent || ctrl.selectedRowsEvent.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			modalService.openCustomDialogs({
				'params' : ctrl.selectedRowsEvent[0],
				'dialogsModule' : 'cda.service.serviceEvent'
			});
		};
		ctrl.searchEvent = function() {
			ctrl.searchCriteriaEvent.list = [];
			ctrl.searchCriteriaEvent.serviceCode = $filter('encode')(serviceInfo.serviceCode);
			authService.searchEvent(ctrl.searchCriteriaEvent).then(
			function(response) {
				angular.merge(ctrl.searchCriteriaEvent, response.data);
				gridService.updateGridOptions(ctrl.searchCriteriaEvent, ctrl.gridOptionsEvent);
			},angular.noop);
		};
		function paginationChangedEvent(pageNum, pageSize) {
			ctrl.searchCriteriaEvent.pageNum = pageNum;
			ctrl.searchCriteriaEvent.pageSize = pageSize;
			ctrl.searchCriteriaEvent.list = [];
			ctrl.searchEvent();
		}
		function rowSelectionChangedEvent(eventRow, selectedRows) {
			ctrl.selectedRowsEvent = selectedRows;
		}
		function sortChangedEvent(sortColumns) {
			// now just support sort by one column
			ctrl.searchCriteriaEvent.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteriaEvent.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.searchEvent();
		}
	}
})(angular);